//
//  WhoCanSeeViewController.m
//  u76ho
//
//  Created by m-air-01 on 16/1/12.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "WhoCanSeeViewController.h"
#import "MyfightersVC.h"
@interface WhoCanSeeViewController ()
<UITableViewDataSource,UITableViewDelegate,SelectedPeopleDelegate>
{
    UITableView * _tableView;
    NSMutableArray * _dataSource1;
    NSMutableArray * _dataSource2;
    
    NSMutableArray * selectedArr;
    NSString * codes;
}
@end

@implementation WhoCanSeeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self main];
    [self addItems];
    self.navigationItem.title = @"谁可以看";
   // [self addNavgationBar];
    // Do any additional setup after loading the view.
}
-(void)main
{
    codes = @"";
    _dataSource1 = [[NSMutableArray alloc]init];
    _dataSource2 = [[NSMutableArray alloc]init];
    selectedArr = [[NSMutableArray alloc]init];
    [selectedArr addObjectsFromArray:@[@"1",@"0",@"0"]];
    [_dataSource1 addObjectsFromArray:@[@"公开",@"私密",@"部分可见"]];
    [_dataSource2 addObjectsFromArray:@[@"所有人可见",@"仅自己可见",@"选中的朋友可见"]];
    self.view.backgroundColor = [UIColor whiteColor];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate =self;
    [self.view addSubview:_tableView];
    self.automaticallyAdjustsScrollViewInsets = NO;
}
#pragma mark - _tableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *  ID = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    cell.textLabel.text = _dataSource1[indexPath.row];
    cell.textLabel.textColor = UIColorFromRGB(0x333944);
    cell.detailTextLabel.text = _dataSource2[indexPath.row];
    cell.detailTextLabel.textColor = UIColorFromRGB(0x94999f);
    if ([selectedArr[indexPath.row] isEqualToString:@"1"]) {
        if (indexPath.row == 2) {
            
        }
        {
        UIImageView * imgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
        imgV.image = [UIImage imageNamed:@"选中图标"];
        cell.accessoryView = imgV;
        }
    }
    else
    {
        cell.accessoryView = nil;
    }
    if (indexPath.row == 2) {
        UIImageView * imgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 8, 14)];
        imgV.image = [UIImage imageNamed:@"all-跳转图标"];
        cell.accessoryView = imgV;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    int i = (int)indexPath.row;
    for (int j = 0; j<selectedArr.count; j++) {
        if (j == i) {
            [selectedArr replaceObjectAtIndex:j withObject:@"1"];
        }
        else
        {
            [selectedArr replaceObjectAtIndex:j withObject:@"0"];
        }
    }
    [_tableView reloadData];
    if (indexPath.row == 2) {
        MyfightersVC * myfighterVC = [[MyfightersVC alloc]init];
        myfighterVC.isSelectedPeople = YES;
        myfighterVC.delegate = self;
        [self.navigationController pushViewController:myfighterVC animated:YES];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 65;
}
-(void)addItems
{
    UIButton * cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancleBtn.frame = CGRectMake(0, 0, 60, 30);
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancleBtn addTarget:self action:@selector(backItem:) forControlEvents:UIControlEventTouchUpInside];
    [cancleBtn setTitleColor:UIColorFromRGB(0x333944) forState:UIControlStateNormal];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc]initWithCustomView:cancleBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIButton * sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sendBtn.frame = CGRectMake(0, 0, 60, 30);
    [sendBtn setTitle:@"完成" forState:UIControlStateNormal];
    [sendBtn addTarget:self action:@selector(sendBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [sendBtn setTitleColor:UIColorFromRGB(0x6ec844) forState:UIControlStateNormal];
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc]initWithCustomView:sendBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
}
-(void)sendBtnAction:(UIButton *)btn
{
    if ([[selectedArr objectAtIndex:0] isEqualToString:@"1"]) {
        [self.delegate whocanseeDelegateWithtype:@"1" codes:@""];
    }
    else if ([[selectedArr objectAtIndex:1] isEqualToString:@"1"])
    {
        [self.delegate whocanseeDelegateWithtype:@"2" codes:@""];
    }
    else
    {
        [self.delegate whocanseeDelegateWithtype:@"3" codes:codes];
    }
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 自定义的delegate
-(void)selectedPeopleDelegatewithCodes:(NSString *)Codes
{
    codes = Codes;
}
#pragma  mark - 返回按钮图标
-(void)addBackItem{
    UIButton * backItem = [UIButton buttonWithType:UIButtonTypeCustom];
    backItem.frame = CGRectMake(0, 0, 9, 17);
    [backItem setImage:[UIImage imageNamed:@"返回图标"] forState:UIControlStateNormal];
    [backItem addTarget:self action:@selector(backItem:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backItem];
}
-(void)backItem:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
